//
//  MainTabBar.swift
//  Weibo
//
//  Created by 张文文 on 15/5/28.
//  Copyright (c) 2015年 zww. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    override func layoutSubviews() {
        super.layoutSubviews()
        println(self.composedButton)
//        println("\(__FUNCTION__)")
//        println(self.subviews as NSArray)
        let btnCount:CGFloat = 5
        let w = self.bounds.size.width / btnCount
        let h = self.bounds.size.height
        
        let btnFrame = CGRectMake(0,0,w,h)
        var index = 0
        for v in subviews as! [UIView] {
            
            
            if v is UIControl && !(v is UIButton) {
                v.frame = CGRectOffset(btnFrame, CGFloat(index) * w , 0)
                
                index++
                if index == 2
                {
                    index++
                }
            }
        }
        
        
        //撰写+按钮位置
        
        composedButton.frame = CGRectOffset(btnFrame, 2 * w, 0)
        
        
        
    }
    ///   +  按钮   懒加载
    lazy var composedButton: UIButton = {
        
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        // 懒加载的代码是闭包，是提前准备好的代码，在需要的时候执行，闭包中就需要使用 self.
        self.addSubview(btn)
        
        return btn
        
    }()

}
